package com.youbug.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youbug.system.entity.DicType;
import com.youbug.system.entity.Dictionary;
import com.youbug.system.service.IDicTypeService;
import com.youbug.system.service.IDictionaryService;
import com.youbug.common.vo.PageQueryVo;
import com.youbug.common.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author youbug
 * @since 2023-02-12
 */
@RestController
@RequestMapping("/dictionary")
public class DictionaryController {

    @Autowired
    private IDicTypeService dicTypeService;

    @Autowired
    private IDictionaryService dictionaryService;

    @PostMapping("/getDicTypes")
    public R<IPage<DicType>> getDicTypes(@RequestBody PageQueryVo<DicType> pageQueryVo){

        IPage<DicType> dicTypePage = dicTypeService.getDicTypePage(pageQueryVo);

        return R.ok(dicTypePage);
    }

    @GetMapping("/getDicType/{id}")
    public R<DicType> getDicType(@PathVariable String  id){

        DicType dic = dicTypeService.getById(id);

        return R.ok(dic);
    }

    @GetMapping("/getDictionary/{id}")
    public R<Dictionary> getDictionary(@PathVariable String  id){

        Dictionary dictionary = dictionaryService.getById(id);

        return R.ok(dictionary);
    }

    @PostMapping("/add")
    public R<Boolean> add(@RequestBody DicType dicType){

        boolean save = dicTypeService.save(dicType);

        return  R.ok(save);
    }

    @PostMapping("/addItem")
    public R<Boolean> addItem(@RequestBody Dictionary dictionary){

        boolean save = dictionaryService.save(dictionary);

        return  R.ok(save);
    }
    @PostMapping("/updateItem")
    public R<Boolean> update(@RequestBody Dictionary dictionary){

        boolean update = dictionaryService.updateById(dictionary);

        return  R.ok(update);
    }

    @GetMapping("/deleteItem/{id}")
    public R<Boolean> deleteItem(@PathVariable String  id){

        boolean remove = dictionaryService.removeById(id);

        return  R.ok(remove);
    }

    @GetMapping("/delete/{id}")
    public R<Boolean> delete(@PathVariable String  id){

        boolean remove = dicTypeService.removeById(id);

        return  R.ok(remove);
    }

    @PostMapping("/update")
    public R<Boolean> update(@RequestBody DicType dicType){

        boolean save = dicTypeService.updateAndItem(dicType);

        return  R.ok(save);
    }

}

